#include <iostream>
#include <algorithm>

using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;

    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

int getHeight(TreeNode* root) {
    if (root == NULL) {
        return 0;
    }

    int leftHeight = getHeight(root->left);
    int rightHeight = getHeight(root->right);

    return max(leftHeight, rightHeight) + 1;
}

bool isBalanced(TreeNode* root) {
    if (root == NULL) {
        return true;
    }

    int leftHeight = getHeight(root->left);
    int rightHeight = getHeight(root->right);

    if (abs(leftHeight - rightHeight) > 1) {
        return false;
    }

    bool leftBalanced = isBalanced(root->left);
    bool rightBalanced = isBalanced(root->right);

    return leftBalanced && rightBalanced;
}

int main() {
    /*
          1
         / \
        2   3
       / \   \
      4   5   6
             /
            7
    */
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);
    root->left->left = new TreeNode(4);
    root->left->right = new TreeNode(5);
    root->right->right = new TreeNode(6);
    root->right->right->left = new TreeNode(7);

    bool balanced = isBalanced(root);

    if (balanced) {
        cout << "二叉树是平衡二叉树" << endl;
    } else {
        cout << "二叉树不是平衡二叉树" << endl;
    }

    return 0;
}